/*  Game/Army Editor for yALB - Yet Another List Builder (roster Editor for Wargames/tabletop games).
    Copyright (C) 2008-2012  Daniel Ricardo Castro Alvarado

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.*/

#include "categorywidget.h"
#include "ui_categorywidget.h"

#include "collections/categorycollection.h"
#include "other/categorymodel.h"
#include "other/modelcommon.h"

#include <QDebug>
#include <QMessageBox>

CategoryWidget::CategoryWidget(CategoryCollection *categories_, CategoryModel *model, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CategoryWidget)
{
    Q_ASSERT(categories_ && model);
    ui->setupUi(this);
    categories = categories_;

    ui->catList->setModel(model);

    connect(ui->catList->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
            this, SLOT(currentCategoryChanged(QModelIndex,QModelIndex)));

}

CategoryWidget::~CategoryWidget()
{
    delete ui;
}

void CategoryWidget::clearCategoryForm() {
    ui->catName->setText("");
    ui->catKey->setText("");
    ui->catNumericLimit->setValue(0);
    ui->catPointsLimit->setValue(0);
    ui->catPercentageLimit->setValue(0);
}

void CategoryWidget::currentCategoryChanged(const QModelIndex &current, const QModelIndex &previous)
{
    Q_UNUSED(previous)
    if(current.isValid()) { // Is child?
        QString key = ui->catList->model()->data(current, ModelCommon::KeyRole).toString();
        GameCategory category = categories->get(key);
        ui->catName->setText(category.getName());
        ui->catKey->setText(key);
        ui->catNumericLimit->setValue(category.getNumericLimit());
        ui->catPointsLimit->setValue(category.getPointLimit());
        ui->catPercentageLimit->setValue(category.getPercentageLimit());
        ui->catDelete->setEnabled(true);
        ui->catUpdate->setEnabled(true);
    } else {
        clearCategoryForm();
        ui->catDelete->setEnabled(false);
        ui->catUpdate->setEnabled(false);
    }
}

void CategoryWidget::on_catAdd_clicked()
{
    GameCategory category(ui->catName->text(), ui->catNumericLimit->value(),
                          ui->catPointsLimit->value(), ui->catPercentageLimit->value());
    categories->add(category, ui->catKey->text());
}

void CategoryWidget::on_catUpdate_clicked()
{
    GameCategory category(ui->catName->text(), ui->catNumericLimit->value(),
                          ui->catPointsLimit->value(), ui->catPercentageLimit->value());
    QString oldKey = ui->catList->model()->data(ui->catList->currentIndex(), ModelCommon::KeyRole).toString();
    categories->update(oldKey, category, ui->catKey->text());
}

void CategoryWidget::on_catDelete_clicked()
{
    if(QMessageBox::warning(0, tr("Are you sure?"), tr("Deleting a category will make invalid all values that use it. Are you sure you want to continue?"),
                            QMessageBox::Yes | QMessageBox::No, QMessageBox::No) == QMessageBox::Yes)
        categories->remove(ui->catList->model()->data(ui->catList->currentIndex(), ModelCommon::KeyRole).toString());
}

void CategoryWidget::changeEvent(QEvent *event) {
    if(event->type() == QEvent::LanguageChange)
        ui->retranslateUi(this);
    QWidget::changeEvent(event);
}
